package com.example.educationcommunity.controller;

import com.example.educationcommunity.dto.CommentDTO;
import com.example.educationcommunity.dto.QuestionDTO;
import com.example.educationcommunity.mapper.CommentMapper;
import com.example.educationcommunity.mapper.LoveMapper;
import com.example.educationcommunity.mapper.QuestionMapper;
import com.example.educationcommunity.mapper.UserMapper;
import com.example.educationcommunity.model.*;
import com.example.educationcommunity.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LikeController {

    @Autowired
    private LoveMapper loveMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/like/{id}/{type}")
    private String like(@PathVariable(name = "id") Integer id,
                        @PathVariable(name = "type") Integer type,
                        HttpServletRequest request,
                        Model model){

        User user = (User) request.getSession().getAttribute("user");


        LoveExample example = new LoveExample();

        example.createCriteria()
                .andParentIdEqualTo(id)
                .andTypeEqualTo(type)
                .andLikerIdEqualTo(user.getId());
        List<Love> loves = loveMapper.selectByExample(example);
        if(loves.size()==0){
            Love love = new Love();
            love.setIsLike(true);
            love.setLikerId(user.getId());
            love.setParentId(id);
            love.setType(type);
            Question question = new Question();
            if(type.equals(1)){
                question = questionMapper.selectByPrimaryKey(id);
                user = userMapper.selectByPrimaryKey(question.getCreator());
                question.setLikeCount(question.getLikeCount()+1);
                question.setGmtModified(System.currentTimeMillis());
                questionMapper.updateByPrimaryKey(question);
                List<CommentDTO> commentDTOList = commentService.getByParentIdList(id);
                model.addAttribute("comments",commentDTOList);
            }else{
                Comment comment = commentMapper.selectByPrimaryKey(id);
                question = questionMapper.selectByPrimaryKey(comment.getParentId());
                user = userMapper.selectByPrimaryKey(question.getCreator());
                comment.setLikeCount(comment.getLikeCount()+1);
                comment.setGmtModified(System.currentTimeMillis());
                commentMapper.updateByPrimaryKey(comment);
                List<CommentDTO> commentDTOList = commentService.getByParentIdList(comment.getParentId());
                model.addAttribute("comments",commentDTOList);
            }
            QuestionDTO questionDTO =new QuestionDTO();
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            model.addAttribute("question",questionDTO);
            loveMapper.insert(love);
            return "question";
        }else {
            Question question = new Question();
            if(type.equals(1)){
                question = questionMapper.selectByPrimaryKey(id);
                user = userMapper.selectByPrimaryKey(question.getCreator());
                List<CommentDTO> commentDTOList = commentService.getByParentIdList(id);
                model.addAttribute("comments",commentDTOList);
            }else{
                Comment comment = commentMapper.selectByPrimaryKey(id);
                question = questionMapper.selectByPrimaryKey(comment.getParentId());
                user = userMapper.selectByPrimaryKey(question.getCreator());
                List<CommentDTO> commentDTOList = commentService.getByParentIdList(comment.getParentId());
                model.addAttribute("comments",commentDTOList);
            }
            QuestionDTO questionDTO =new QuestionDTO();
            BeanUtils.copyProperties(question,questionDTO);
            questionDTO.setUser(user);
            model.addAttribute("question",questionDTO);
            model.addAttribute("error", "已经赞过了");
            return "question";
        }
    }
}
